package com.fqgj.xjd.user.service.impl;
/**
 * Created by zhangxingyun on 17/7/23.
 */

import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserAuthAccountService;
import com.fqgj.xjd.user.dao.UserAuthAccountDao;
import com.fqgj.xjd.user.client.response.UserAuthAccount;
import com.fqgj.xjd.user.entity.UserAuthAccountEntity;
import com.fqgj.xjd.user.client.enums.UserAccountTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Description: 认证账户维护服务
 * @Author zhangxingyun
 * @Create 2017-07-23 15:44
 */
@Service("userAuthAccountService")
public class UserAuthAccountServiceImpl implements UserAuthAccountService {

    @Autowired
    UserAuthAccountDao userAuthAccountDao;

    /**
     * 根据账户名获取账户
     * @param accountName
     * @param userAccountTypeEnum
     * @return
     */
    @Override
    public Response<UserAuthAccount> getUserAuthAccountByAccountName(String accountName, UserAccountTypeEnum userAccountTypeEnum) {
        UserAuthAccountEntity userAuthAccountEntity =userAuthAccountDao.selectUserAuthAccountByAccountNameAndType(accountName,userAccountTypeEnum.getDesc());

        if(null!=userAuthAccountEntity){
            UserAuthAccount userAuthAccount = new UserAuthAccount();
            BeanUtils.copyProperties(userAuthAccountEntity,userAuthAccount);;
            return Response.ok().putData(userAuthAccount);
        }
        return Response.ok();
    }

    /**
     * 获取用户名下所有账户信息
     * @param userCode
     * @return
     */
    @Override
    public Response<UserAuthAccount> getUserAuthAccountListByUserCode(String userCode) {
        List<UserAuthAccountEntity> userAuthAccountEntityList = userAuthAccountDao.selectUserAuthAccountListByUserCode(userCode);

        if(userAuthAccountEntityList.size()>0){
            List<UserAuthAccount> userAuthAccountList = new ArrayList<>();
            for(UserAuthAccountEntity userAuthAccountEntity:userAuthAccountEntityList){
                UserAuthAccount userAuthAccount = new UserAuthAccount();
                BeanUtils.copyProperties(userAuthAccountEntity,userAuthAccount);
                userAuthAccountList.add(userAuthAccount);
                return Response.ok().putData(userAuthAccount);
            }
        }
        return Response.ok();
    }

    /**
     * 判断用户认证账户认证状态
     *
     * @param userCode
     * @param userAccountTypeEnum
     * @param checkPassword
     * @return
     */
    @Override
    public Response<UserAuthAccount> getUserAuthAccountByUserCode(String userCode, UserAccountTypeEnum userAccountTypeEnum, Boolean checkPassword) {

        UserAuthAccountEntity userAuthAccountEntity =userAuthAccountDao.selectUserAuthAccountByUserCode(userCode,userAccountTypeEnum.getDesc(),checkPassword);

        if(null!=userAuthAccountEntity){
            UserAuthAccount userAuthAccount = new UserAuthAccount();
            BeanUtils.copyProperties(userAuthAccountEntity,userAuthAccount);;
            return Response.ok().putData(userAuthAccount);
        }
        return Response.ok();
    }

    /**
     * 保存认证账户信息
     *
     * @param userAuthAccount
     * @return
     */
    @Override
    public Response<Boolean> saveUserAuthAccount(UserAuthAccount userAuthAccount) {
        UserAuthAccountEntity userAuthAccountEntity = new UserAuthAccountEntity();
        BeanUtils.copyProperties(userAuthAccount,userAuthAccountEntity);
        userAuthAccountDao.insert(userAuthAccountEntity);
        return Response.ok();
    }
}
